﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebUS;
using WebDS;
using WebDS.CDBNames;
using IP.Core.IPCommon;
using IP.Core.IPUserService;
using IP.Core.IPSystemAdmin;

public partial class ChucNang_Thong_tin_cong_tac_cac_nhan : System.Web.UI.Page
{
    private object locker = new object();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                lock (locker)
                {
                    load_cbo_nhan_vien();    
                }

                if (Request.QueryString["id_use"] != null)
                {
                    m_dc_id_use = CIPConvert.ToDecimal(Request.QueryString["id_use"]);
                    load_thong_tin_nhan_vien_to_form();
                }
                else
                {
                    // Đoạn code lỗi, vì trong bảng V_US_DM_NHAN_VIEN không lưu thông tin của user admin.
                    m_us_nhan_vien.Get_us_by_ten_truy_cap(Session["UserName"].ToString());
                    m_dc_id_use = m_us_nhan_vien.dcID;
                    load_thong_tin_nhan_vien_to_form();
                }
            }
            catch (Exception ex)
            {
                ;
            }
        }
    }
    #region Members
    US_V_DM_NHAN_VIEN m_us_nhan_vien = new US_V_DM_NHAN_VIEN();
    decimal m_dc_id_use = 0;
    #endregion

    #region Private Methods
    private string get_thong_tin_nghi_phep_by_nhan_vien(decimal ip_id_use)
    {
        decimal v_so_ngay_nghi = 0;
        string v_thong_tin_nghi_phep = "";
        US_V_GD_NHAN_VIEN_NGHI_PHEP v_us_nghi_phep = new US_V_GD_NHAN_VIEN_NGHI_PHEP();
        v_us_nghi_phep.Get_thong_tin_by_use(m_dc_id_use, DateTime.Today);
        v_thong_tin_nghi_phep = "Đã nghỉ " + v_so_ngay_nghi + " ngày, còn được nghỉ " + (12 - v_so_ngay_nghi).ToString() + " ngày";
        return v_thong_tin_nghi_phep;
    }
    private void load_thong_tin_nhan_vien_to_form()
    {
        US_CM_DM_TU_DIEN v_us_chuc_vu, v_us_phong_ban;
        string v_str_vi_tri = "";
        if (m_dc_id_use != 0)
        {
            m_us_nhan_vien = new US_V_DM_NHAN_VIEN(m_dc_id_use);
            m_txt_ma_tv.Text = m_us_nhan_vien.strMA_NHAN_VIEN;
            m_cbo_user.SelectedValue = m_dc_id_use.ToString();
            if (!m_us_nhan_vien.IsCHUC_VUNull())
            {
                v_us_chuc_vu = new US_CM_DM_TU_DIEN(m_us_nhan_vien.dcCHUC_VU);
                v_str_vi_tri += v_us_chuc_vu.strTEN;
            }
            if (!m_us_nhan_vien.IsPHONG_BANNull())
            {
                v_us_phong_ban = new US_CM_DM_TU_DIEN(m_us_nhan_vien.dcPHONG_BAN);
                v_str_vi_tri += " - " + v_us_phong_ban.strTEN;
            }
            m_txt_vi_tri_cong_tac.Text = v_str_vi_tri;

            m_txt_nghi_phep.Text = get_thong_tin_nghi_phep_by_nhan_vien(m_dc_id_use);

            if(!string.IsNullOrEmpty(m_us_nhan_vien.strANH_CA_NHAN))
                m_img_anh_tv.ImageUrl = m_us_nhan_vien.strANH_CA_NHAN;
        }
    }

    private void load_cbo_nhan_vien()
    {
        try
        {
            US_V_DM_NHAN_VIEN v_us_nv = new US_V_DM_NHAN_VIEN();
            DS_V_DM_NHAN_VIEN v_ds_nv = new DS_V_DM_NHAN_VIEN();
            v_us_nv.FillDataset(v_ds_nv);
            m_cbo_user.DataSource = v_ds_nv.V_DM_NHAN_VIEN;
            m_cbo_user.DataTextField = V_DM_NHAN_VIEN.HO_VA_TEN;
            m_cbo_user.DataValueField = V_DM_NHAN_VIEN.ID;
            m_cbo_user.DataBind();
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }
    #endregion

    #region Events

    #endregion
    protected void OnSelectedMethod(object sender, EventArgs e)
    {
        try
        {
            lock (locker)
            {
                Response.Redirect(string.Format("{0}?id_use={1}", HttpContext.Current.Request.Url.AbsolutePath, m_cbo_user.SelectedValue));
            }
        }
        catch (Exception ex)
        {

            throw;
        }
    }
}